import { isDef, inBrowser } from '@src/util/index'
import { isNumeric } from '@src/util/validate/number'

export function addUnit (value?: string | number): string | undefined {
  if (!isDef(value)) {
    return undefined
  }
  
  value = String(value)
  return isNumeric(value) ? `${value}px` : value
}

// cache
let rootFontSize: number

function getRootFontSize () {
  if (!rootFontSize) {
    const doc = document.documentElement
    const fontSize = doc.style.fontSize || window.getComputedStyle(doc).fontSize
    rootFontSize = parseFloat(fontSize)
  }
  
  return rootFontSize
}

function convertRem (value: string) {
  value = value.replace(/rem/g, '')
  return +value * getRootFontSize()
}

function convertVw (value: string) {
  value = value.replace(/vw/g, '')
  return (+value * window.innerWidth) / 100
}

export function unitToPx (value: string | number): number {
  if (typeof value === 'number') {
    return value
  }
  
  if (inBrowser) {
    if (value.indexOf('rem') !== -1) {
      return convertRem(value)
    }
    
    if (value.indexOf('vw') !== -1) {
      return convertVw(value)
    }
  }
  
  return parseFloat(value)
}
